#######################################################################
#
# XBoxMpd - MPD client script for the XBMC
# I am not affiliated with XBMC, but here is their site: www.xbmc.org
# Copyright (C) 2009 Dave Clark
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#######################################################################


# import the XBMC libraries so we can use the controls and functions of XBMC
import lib.mpdclient2 as mpdclient2
import lib.sonata.main as sonata_main
import xbmc, xbmcgui
import os

#get actioncodes from keymap.xml
ACTION_PREVIOUS_MENU = 10
ACTION_SELECT_ITEM = 7
ACTION_PLAY = 79

ROOT_DIR = os.getcwd()

CONTROL_BUTTON_CURRENT=101
CONTROL_BUTTON_LIBRARY=102
CONTROL_BUTTON_PLAYLISTS=103
CONTROL_BUTTON_INFO=104
CONTROL_BUTTON_EXIT=105

CONTROL_CURRENT=50
CONTROL_PLAYLISTS=51

CONTROL_LABEL_CURSONG=21
CONTROL_LABEL_CURARTIST=22
CONTROL_LABEL_CURALBUM=23

REFRESH_GUI_CURRENT=1

class Rect:
    def __init__(self, x=0, y=0, w=0, h=0):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
 
class XBoxMpd(xbmcgui.WindowXML):
    
    def onInit(self):
	# Run main from Sonata
    	app = sonata_main.Base(args)
	try:
	    app.main()
	except KeyboardInterrupt:
	    pass

    def onAction(self, action):
        if action == ACTION_PREVIOUS_MENU:
            self.close()

    def onClick(self, controlID):
        ctrl = self.getControl(controlID)

        if controlID == CONTROL_BUTTON_CURRENT:
            pass
        elif controlID == CONTROL_BUTTON_LIBRARY:
            pass
        elif controlID == CONTROL_BUTTON_PLAYLISTS:
            pass
        elif controlID == CONTROL_BUTTON_INFO:
            pass
        elif controlID == CONTROL_BUTTON_EXIT:
            self.close()
                
    def onFocus(self, controlID):
        pass

    def init_current(self):
        self.currentPlaylist = self.mpd.playlistinfo()
        playlist = self.getControl(CONTROL_CURRENT)
        playlist.reset()

        # Get only the file name, not the directory
        for i in self.currentPlaylist:
            (dirName, fileName) = os.path.split(i.file)
            playlist.addItem(fileName)

    def init_playlists(self):
        self.playlistList = self.mpd.listplaylists()
        playlist = self.getControl(CONTROL_PLAYLISTS)
        playlist.reset()

        for i in self.playlistList:
            playlist.addItem(i.playlist)

    def RefreshGui(self,type):
        # Update the specified section of the screen
        if type == REFRESH_GUI_CURRENT:
            info = self.mpd.currentsong()
            self.getControl(CONTROL_LABEL_CURSONG).setLabel(info.title)
            self.getControl(CONTROL_LABEL_CURARTIST).setLabel(info.artist)
            self.getControl(CONTROL_LABEL_CURALBUM).setLabel(info.album)

    def exit_all(self):
        # Shut off the idle thread
        self.getControl(CONTROL_LABEL_CURSONG).setLabel("step1")
        self.idleThread.stop(self.getControl(CONTROL_LABEL_CURSONG))
        self.getControl(CONTROL_LABEL_CURSONG).setLabel("woo")
        #self.mpd.noidle()

        # Wait until the threads are finished
        #self.idleThread.join()
        #self.close()
 
w = XBoxMpd("xboxmpd.xml", ROOT_DIR, "DefaultSkin")
w.doModal()
del w
